<?php

/*
 * 
 * this is main.php
 * part of the dynasties2 project on www.dixieandtheninjas.net
 * 
 * created 5/31/12
 * 
 */

// check user login




class Test extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

            $this->load->helper(array('form','url'));
            $this->load->library('form_validation');
	}
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function index() {
            
            $time_start = microtime(true);
            
            $time_end = microtime(true);
            $time = $time_end - $time_start;
            //echo "<br>Load Time:  " . round($time, 3) . " seconds\n";
            
        } /// end public function index()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function probability1() {
            
            // lets assume 2d2
            $combinations = '0';
            $dice = '3';
            $sides = '6';
            
            $array = array();
            $alldice = array();
            
            while ($dice > '0') {
                //$array[] = $dice;
                $alldice[] = $sides;
                $dice--;
            }
            
            print_r($alldice);
            
            //print_r($array);
            
           
                foreach ($alldice as $val) {
                    while ($val > '0') {
                        echo "<br>Rolling a dice: $val!";
                        $val--;
                    }
                    //echo "<br>Rolling a dice!";
                    

                }
            
            
            echo "<br>Combinations: $combinations";
        } /// end public function index()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        public function map_testing1() {
            session_start();
            
            //echo "test";
            
            /////////////////////////////////////////////////////////////////////////////////////////////////////
            //////////////   map part    //////////////////////////////////////////////////////////////////
            $map = "applicationFolder/map1small.gif";
                    
            if(file_exists($map)){ 
                $img = ImageCreateFromGIF($map);

                $border = ImageColorAt ($img, 51, 67);
                
                $sea_blue = ImageColorAllocate($img, 135, 206, 250);
                
                ImageFillToBorder($img, 390, 90, $border, $sea_blue); //ocean
                $color1 = mt_rand(0,255);
                $color2 = mt_rand(0,255);
                $color3 = mt_rand(0,255);
                $random_color = ImageColorAllocate($img, $color1, $color2, $color3);
                
                //$character_id = $_SESSION["active_character"];
                $character_id = '17571';
                $title_data = $this->doctrine->em->getRepository('ORM\Dynasties2\Titles')->findBy(array('characterLink' => $character_id));
                
                foreach ($title_data as $row) {
                    
                    $coordx = $row -> getRegionLink()->getCoordX();
                    $coordy = $row -> getRegionLink()->getCoordY();
                    
                    ImageFillToBorder($img, $coordx, $coordy, $border, $random_color);
                    
                }
                
                header("Content-type: image/gif");
                ImageGIF($img); 
                ImageDestroy($img);  // Free memoryu
                
              
    
    /*
    //  <img src="data:image/x-icon;base64,<?php echo base64_encode($img); ?>"></img>
     * 
     */
            } else {
                echo "<br>No image loaded.</br>";
            }
            //////////////////////////////////////////////////// end map part /////////////////////////////////////
            
            
        } //// end public function map_testing1()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function map_testing2() {
            
            echo "<br>test!</br>";
            echo "Look some data is here, holy cow!</br>";
            
            ?>

                <img src="map_testing1" border ="5" height ="600"/>

            <?php
            
        } //// end public function map_testing1()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        public function war_test_stuff() {
            
            
            $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Wars u 
                        
                            ");

                $wars = $query->getResult();
                
                
                $allcombatants = array();
                
                
                foreach ($wars as $row) {
                    
                    $attacker = $row -> getAttacker();
                    $defender = $row -> getDefender();
                    
                    $allattackers[] = $attacker;
                    $alldefenders[] = $defender;
                    
                }
                
                
                
                foreach ($allattackers as $row) {
                
                    // get their regions from TITLES
                    $query = $this->doctrine->em->createQuery("SELECT u 
                        FROM ORM\Dynasties2\Titles u 
                        WHERE u.characterLink = $row
                        ");

                    $unique_rulers = $query->getResult();

                    foreach ($unique_rulers as $row2) {

                        //echo $row2->getRegionLink()->getId();
                        ///// new code loaded, not yet working, go back further.

                    // put display code here:

                        $coordx = $row2 -> getRegionLink()->getCoordX();
                        $coordy = $row2 -> getRegionLink()->getCoordY();

                        ImageFillToBorder($img, $coordx, $coordy, $border, $firebrick);

                    }
                    
                } /// end foreach allattackers
            
                //////////////////////////////////////////////////
                /////////////////// check to see if any attackers are also defenders!!!
                $attack_and_defend = array();
                
                foreach ($allattackers as $row) {
                    
                    //$attack_and_defend[] = array_keys($alldefenders, $row);
                    echo "<br>Searching alldefenders for $row</br>";
                    //print_r(array_keys($alldefenders, $row));
                    //print_r($attack_and_defend);
                    if (array_keys($alldefenders, $row)) {
                        $test_array[] = array_keys($alldefenders, $row);
                        //print_r($test_array);
                    }
                    
                    
                }
                echo "<br>******</br>";
                foreach ($test_array as $row) {
                    //print_r($row);
                    foreach ($row as $row2) {
                        echo "<br>$row2 is the key to a row in alldefenders that is attacking AND defending!  " . $alldefenders[$row2];
                        //$character_data = $this->doctrine->em->find('ORM\Dynasties2\Titles', $row->getWhichRegion());
                        $titles_data = $this->doctrine->em->getRepository('ORM\Dynasties2\Titles')->findOneBy(array('characterLink' => $alldefenders[$row2]));
                        $coordx = $titles_data->getCoordX();
                        $coordy = $titles_data->getCoordY();
                        ImageFillToBorder($img, $coordx, $coordy, $border, $dimgray);
                    }
                }
                
                
                
                /////////////////////////////////// end check to see if any attackers are also defenders
            
            
            
        } //// end public function war_test_stuff()
////////////////////////////////////////////////////////////////////////////////////////////////


        
       
        
} //////// end session security check

        
